package q917_reverseOnlyLetters;

public class Solution {
    /*
    利用双指针遍历并交换即可
     */
    public String reverseOnlyLetters(String s) {
        int l = 0, r = s.length() - 1;
        char[] chars = s.toCharArray();
        while (l < r) {
            while (l < r && !Character.isLetter(chars[l])) ++l;
            while (l < r && !Character.isLetter(chars[r])) --r;
            char temp = chars[l];
            chars[l] = chars[r];
            chars[r] = temp;
            ++l;
            --r;
        }
        return new String(chars);
    }
}
